# -*- coding: utf-8 -*-
#!/usr/bin/python
# -*- coding:utf-8 -*-
import os,sys,thread
reload(sys) 
sys.setdefaultencoding('utf8')
#sys.path.append("./dmfund")
sys.path.append('/var/www/dadeinvest/dmfund/')
#sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
import hashlib
import web
from debug import logprint,logger
import dm
import random

#reload(sys) 
#sys.setdefaultencoding('utf8')
#sys.path.append("./buffettfaq")
#import faqmodel


web.config.debug = True 

urls = (
    '', 'Index',    
    '/', 'Index',
    '/index','Index',
    )



#app_root = os.path.dirname(__file__)
#db=model.db
#webdb=model.webdb
#URL开始



#from bae.core.wsgi import WSGIApplication
#application = WSGIApplication(app.wsgifunc())

t_globals = {
    'datestr': web.datestr,
    'str':str,
    'random':random,
}

app_root = os.path.dirname(__file__)
templates_root = os.path.join(app_root, './templates/')
render = web.template.render('/var/www/dadeinvest/dmfund/templates/', base='base', globals=t_globals,cache = False)
    
class Index:

    def GET(self):
        """ Show page """
        user_data=web.input()
        if 'code' in user_data.keys():
            code=user_data.code
            wpm=dm.get_fund_pm(code,15,'tlpm_week')
            mpm=dm.get_fund_pm(code,15,'tlpm_month')
            ranks=dm.get_fund_ranks(code,15)
            return render.fund(wpm,mpm,ranks)
        else:
            try:
                result=dm.indexpage()
                lastrank=result[0]
                allrank=result[1]
                return render.index(lastrank,allrank)
            #return "test OK le"
            except Exception,e:
                return logger.info(str(e))

app = web.application(urls, globals(), autoreload=True)

### Templates

###

#templates_root = os.path.join(app_root, 'dmfund/templates/')
#render = web.template.render(templates_root)

        
application = app.wsgifunc() 

